package com.ambulance.project.biz.util.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/***
 *  @description
 *  @author z
 *  @date: 2020/9/21
 *  @version: v1.0
 */
public class ParseXmldom4j
{


	public static void main(String[] args) throws DocumentException
	{
		String xmlpath = "/Users/zhao/Documents/坤渔/5G救护车资源文档/迈瑞ECG数据/MR2018020720175504_20180207201811.xml";
		Map<String, Object> stringObjectMap = parseMaiRuiXml(xmlpath);
	}

	public static Map<String, Object> parseMaiRuiXml(String xmlpath)
	{
		SAXReader reader = new SAXReader();
		Document document = null;
		Map<String, Object> rlt = new HashMap<String, Object>();
		try
		{
			document = reader.read(xmlpath);
		} catch (DocumentException e)
		{
			e.printStackTrace();
			return rlt;
		}

		Element rootEle = document.getRootElement();
		Element twelveLeadReport = rootEle.element("VitalSigns").element("TwelveLeadReport");
		List<Element> observations = twelveLeadReport.element("Observations").elements("Observation");
		for (Element e : observations)
		{
			String type = e.attributeValue("Type");
			if (null != type && ("ECG_VR".equals(type)))
			{
				String value = e.getTextTrim();
				rlt.put("ECG_Heart_Rate", value);
			}

		}
		Element waveforms = twelveLeadReport.element("Waveforms");
		List<Element> waveform = waveforms.elements("Waveform");

		for (Element e : waveform)
		{
			String type = e.attributeValue("Type");
			Element waveformSegment = e.element("WaveformSegment");
			String sampleRate = waveformSegment.element("SampleRate").getTextTrim();
			String dataResolution = waveformSegment.element("DataResolution").getTextTrim();
			String data = waveformSegment.element("Data").getTextTrim();
			List<String> list = Arrays.asList(data.split(",")).stream().map(s -> (s.trim())).collect(Collectors.toList());
			rlt.put(type, list);
			System.out.println(String.format("type: %s,sampleRate: %s,dataResolution: %s,data: %s", type, sampleRate, dataResolution, data));
		}

		return rlt;
	}


}